package cn.itcast.sys.service.impl;

import cn.itcast.goods.feign.DishFegin;
import cn.itcast.goods.feign.SetmealFeign;
import cn.itcast.goods.vo.DishOverViewVO;
import cn.itcast.goods.vo.SetmealOverViewVO;
import cn.itcast.orders.feign.OrderFegin;
import cn.itcast.orders.pojo.Orders;
import cn.itcast.orders.vo.OrderOverViewVO;
import cn.itcast.sys.service.WorkspaceService;
import cn.itcast.sys.vo.BusinessDataVO;
import cn.itcast.users.feign.UserFegin;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
public class WorkspaceServiceImpl implements WorkspaceService {
//
//    @Autowired
//    private OrderMapper orderMapper;
//    @Autowired
//    private SetmealMapper setmealMapper;
//    @Autowired
//    private DishMapper dishMapper;
//    @Autowired
//    private UserMapper userMapper;
    @Autowired
    private OrderFegin orderFegin;
    @Autowired
    private SetmealFeign setmealFeign;
    @Autowired
    private DishFegin dishFegin;
    @Autowired
    private UserFegin userFegin;

    /**
     * 根据时间段统计营业数据
     * @param begin
     * @param end
     * @return
     */
    public BusinessDataVO getBusinessData(LocalDateTime begin, LocalDateTime end) {
        /**
         * 营业额：当日已完成订单的总金额
         * 有效订单：当日已完成订单的数量
         * 订单完成率：有效订单数 / 总订单数
         * 平均客单价：营业额 / 有效订单数
         * 新增用户：当日新增用户的数量
         */

        Map map = new HashMap();
        map.put("beginTime",begin);
        map.put("endTime",end);

        //查询总订单数
        Integer totalOrderCount = 1;//orderMapper.countByMap(map);

        map.put("status", Orders.COMPLETED);
        //营业额
        Double turnover = 1.0; //orderMapper.sumByMap(map);
        turnover = turnover == null? 0.0 : turnover;

        //有效订单数
        Integer validOrderCount = 1;//orderMapper.countByMap(map);

        Double unitPrice = 0.0;

        Double orderCompletionRate = 0.0;
        if(totalOrderCount != 0 && validOrderCount != 0){
            //订单完成率
            orderCompletionRate = validOrderCount.doubleValue() / totalOrderCount;
            //平均客单价
            unitPrice = turnover / validOrderCount;
        }

        //新增用户数
        Integer newUsers = 11;//userMapper.countByMap(map);

        return BusinessDataVO.builder()
                .turnover(turnover)
                .validOrderCount(validOrderCount)
                .orderCompletionRate(orderCompletionRate)
                .unitPrice(unitPrice)
                .newUsers(newUsers)
                .build();
    }

    /**
     * 查询订单管理数据
     *
     * @return
     */
    @Override
    public OrderOverViewVO getOverviewOrders() {

//        OrderOverViewVO orderOverViewVO = orderMapper.getAllOrderStatus();
        OrderOverViewVO orderOverViewVO = orderFegin.getAllOrderStatus();

//        orderOverViewVO.setAllOrders(orderMapper.getAllOrderCount(new HashMap<>()));
        orderOverViewVO.setAllOrders(orderFegin.getAllOrderCount(new HashMap<>()));

        return orderOverViewVO;
    }

    /**
     * 查询套餐总览
     *
     * @return
     */
    @Override
    public SetmealOverViewVO getOverviewSetmeals() {
//        SetmealOverViewVO setmealOverViewVO = setmealMapper.getPackageOverview();
        SetmealOverViewVO setmealOverViewVO = setmealFeign.getPackageOverview();

        return setmealOverViewVO;
    }

    /**
     * 查询菜品总览
     *
     * @return
     */
    @Override
    public DishOverViewVO getOverviewDishes() {
//        DishOverViewVO dishOverViewVO = dishMapper.getOverviewDishes();
        DishOverViewVO dishOverViewVO = dishFegin.getOverviewDishes();

        return dishOverViewVO;
    }
}

